<?php
declare(strict_types=1);

namespace app\controller\api\admin\system;

use think\App;
use think\Request;
use think\exception\ValidateException;

use app\controller\api\ApiBaseController;
use app\logic\api\admin\system\RoleLogic;
use app\validate\api\admin\system\RoleValidate;

class RoleController extends ApiBaseController
{
    protected RoleLogic $logic;

    public function __construct(App $app)
    {
        parent::__construct($app);
        $this->logic = new RoleLogic();
    }

    public function index(Request $request)
    {
        $param = $request->param();

        $where = [];
        // isset($param['id']) && $param['id'] != '' && $where[] = ['id', '=', $param['id']];
        isset($param['title']) && $param['title'] != '' && $where[] = ['title', 'like', '%' . $param['title'] . '%'];
        isset($param['code']) && $param['code'] != '' && $where[] = ['code', 'like', '%' . $param['code'] . '%'];
        isset($param['status']) && $param['status'] != '' && $where[] = ['status', '=', $param['status']];

        $lgc_re = $this->logic->list($where, '*', [], false);

        return $lgc_re['code'] == 200
            ? $this->success($lgc_re['msg'], $lgc_re['data'])
            : $this->error($lgc_re['msg']);
    }

    public function save(Request $request)
    {
        $data = $request->only([
            'parent_id' => 0,
            'title',
            'status' => 1,
            'remark',
            'code',
            'sort' => 99,
            // 'data_scope' => null, // setDataScope方法中处理
        ]);

        try {
            validate(RoleValidate::class)->scene('save')->check($data);

            $lgc_re = $this->logic->save($data);

            return $lgc_re['code'] == 200
                ? $this->success($lgc_re['msg'])
                : $this->error($lgc_re['msg']);
        } catch (ValidateException $e) {
            return $this->error($e->getMessage());
        }
    }

    public function read($id)
    {
        $lgc_re = $this->logic->read(['id' => $id]);

        return $lgc_re['code'] == 200
            ? $this->success($lgc_re['msg'], $lgc_re['data'])
            : $this->error($lgc_re['msg']);
    }

    public function update(Request $request, $id)
    {
        $data = $request->only([
            'parent_id' => 0,
            'title',
            'status',
            // 'rules', // setRules方法中处理
            'remark',
            'code',
            'sort' => 99,
            // 'data_scope' => null, // setDataScope方法中处理
        ]);
        $data['id'] = $id;

        try {
            validate(RoleValidate::class)->scene('update')->check($data);

            $lgc_re = $this->logic->update($data);

            return $lgc_re['code'] == 200
                ? $this->success($lgc_re['msg'])
                : $this->error($lgc_re['msg']);
        } catch (ValidateException $e) {
            return $this->error($e->getMessage());
        }
    }

    public function delete($id)
    {
        $data = $this->request->only([
            'del_ids'
        ]);

        try {
            validate(RoleValidate::class)->scene('delete')->check($data);

            $lgc_re = $this->logic->delete($data);

            return $lgc_re['code'] == 200
                ? $this->success($lgc_re['msg'])
                : $this->error($lgc_re['msg']);
        } catch (ValidateException $e) {
            return $this->error($e->getMessage());
        }
    }

    public function setRules(int $id, Request $request)
    {
        $data = $request->only([
            'parent_id', // 特意设计-用来参与验证,避免越权
            'rules',
        ]);
        $data['id'] = $id;

        try {
            validate(RoleValidate::class)->scene('setRules')->check($data);

            $lgc_re = $this->logic->setRules($data);

            return $lgc_re['code'] == 200
                ? $this->success($lgc_re['msg'])
                : $this->error($lgc_re['msg']);
        } catch (ValidateException $e) {
            return $this->error($e->getMessage());
        }
    }


    public function changeDept()
    {
        $data = $this->request->only([
            'ids',
            'dept_id'
        ]);

        try {
            validate(RoleValidate::class)->scene('changeDept')->check($data);

            $lgc_re = $this->logic->changeDept($data);

            return $lgc_re['code'] == 200
                ? $this->success($lgc_re['msg'])
                : $this->error($lgc_re['msg']);
        } catch (ValidateException $e) {
            return $this->error($e->getMessage());
        }
    }


    public function setDataScope(Request $request)
    {
        $data = $request->only([
            'id',
            'parent_id', // 特意设计-用来参与验证,避免越权
            'data_scope' => [
                'type' => \zhtp\Auth::DATA_SCOPE['SELF'][0],
                'custom' => []
            ]
        ]);

        try {
            validate(RoleValidate::class)->scene('setDataScope')->check($data);

            $lgc_re = $this->logic->setDataScope($data);

            return $lgc_re['code'] == 200
                ? $this->success($lgc_re['msg'])
                : $this->error($lgc_re['msg']);
        } catch (ValidateException $e) {
            return $this->error($e->getMessage());
        }
    }
}
